/*
* (C) Copyright 2013 Philip Kirchhoff 
* 
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
* 
*-------------------------------------------------------------------------------
* This file is part of the DataLogger library by Philip Kirchhoff
* https://code.google.com/p/pk-data-logger/
* SVN: http://pk-data-logger.googlecode.com/svn/trunk
*-------------------------------------------------------------------------------
* Module    : DlFileReader.cpp   
* Author    : Kirchhoff, Ph01.04.201309.08.2012
*-------------------------------------------------------------------------------
* Function definitions for class DlFileReader
*   
*-------------------------------------------------------------------------------
* Change History:
* 01.04.2013  Kirchhoff 	Created
--------------------------------------------------------------------------------
*/

#include <iostream>
#include <iomanip>
#include <fstream>
#include <stdexcept>
#include <string>
#include <sstream>

#include "DlFileReader.h"
#include "DlDataset.h"
#include "DlDataChannel.h"

using namespace std;

DlFileReader::DlFileReader(const char *fileName)
    : DlFileHandler(fileName)
{
}

/**************************************************************************//**
* @brief Read dataset from file
******************************************************************************/
void DlFileReader::readData()
{
    if(!mpDataSet)
    {
        string excStr(__PRETTY_FUNCTION__);
        excStr += ": No dataset specified";
        throw runtime_error(excStr.c_str());
    }

    if(mFileFormat == FILE_FORMAT_CSV_STD)
        readCsvStdFile();
}

/**************************************************************************//**
* @brief Read dataset from CSV file
******************************************************************************/
void DlFileReader::readCsvStdFile()
{
    if(!mpDataSet)
        return;

    // Open File
    ifstream inFile;
    inFile.open(mFileName.c_str(), ios_base::in);

    // File open?
    if(!inFile.is_open())
    {
        string excStr(__PRETTY_FUNCTION__);
        excStr += ": Could not open " + mFileName;
        throw runtime_error(excStr.c_str());
    }

    string lineStr;
    stringstream sstr;
    unsigned int pos0;
    unsigned int numChan = 0;

    // Read format identifier
    getline(inFile, lineStr);
    if(lineStr != "CSV_STD_V1")
    {
        string excStr(__PRETTY_FUNCTION__);
        excStr += ": Unsupported format: " + lineStr;
        throw runtime_error(excStr.c_str());
    }

    // Get channel info
    getline(inFile, lineStr);
    pos0 = lineStr.find_first_of("NUM_CHAN=");
    if(pos0 == string::npos)
    {
        string excStr(__PRETTY_FUNCTION__);
        excStr += ": Missing channel number identifier";
        throw runtime_error(excStr.c_str());
    }
    lineStr.erase(0, pos0+1);
    sstr << lineStr;
    sstr >> numChan;



//    // Write Channel Info
//    outFile << "NUM_CHAN=" << mpDataSet->numChannnels() << endl;

//    // Write channel IDs
//    inFile << "sample;" << "timestamp;";
//    for(unsigned int i = 0; i < mpDataSet->numChannnels(); i++)
//    {
//        inFile << mpDataSet->channel(i)->channelId() << ";";
//    }
//    inFile << endl;

//    // Write channel type
//    inFile << ";" << ";";
//    for(unsigned int i = 0; i < mpDataSet->numChannnels(); i++)
//    {
//        inFile << mpDataSet->channel(i)->channelTypeString() << ";";
//    }
//    inFile << endl;


//    // Write channel units
//    inFile << ";" << ";";
//    for(unsigned int i = 0; i < mpDataSet->numChannnels(); i++)
//    {
//        inFile << mpDataSet->channel(i)->channelUnit() << ";";
//    }
//    inFile << endl;

//    // Write channel description
//    inFile << ";" << ";";
//    for(unsigned int i = 0; i < mpDataSet->numChannnels(); i++)
//    {
//        inFile << mpDataSet->channel(i)->channelDescription() << ";";
//    }
//    inFile << endl;


//    // Write channel data
//    unsigned int *sampleNumArr = new unsigned int[mpDataSet->numChannnels()];
//    try
//    {
//        // Get number of samples for each channel
//        for(unsigned int i = 0; i < mpDataSet->numChannnels(); i++)
//        {
//            sampleNumArr[i] = mpDataSet->channel(i)->numSamples();
//            //cout << "Channel " << i << " has " << sampleNumArr[i] << " samples\n";
//        }

//        unsigned int sampleCnt = 0;
//        bool dataWritten;
//        do
//        {
//            dataWritten = false;

//            inFile << sampleCnt << ";";
//            inFile << mpDataSet->offsetForSample(sampleCnt) << ";";
//            for(unsigned int i = 0; i < mpDataSet->numChannnels(); i++)
//            {
//                if(sampleCnt < sampleNumArr[i])
//                {
//                     inFile << mpDataSet->channel(i)->sampleString(sampleCnt) << ";";
//                     dataWritten = true;
//                }
//                else
//                {
//                    inFile << ";";
//                }
//            }
//            inFile << endl;
//            sampleCnt++;
//        }while(dataWritten);
//    }
//    catch(std::exception &e)
//    {
//        delete[] sampleNumArr;
//        throw e;
//    }

//    inFile << endl;

    inFile.close();
}
